import { Component, OnInit, Input, EventEmitter, Output, Host, Inject, forwardRef, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';

interface Response {
  result: string[];
  result_code: string;
  message: string;
  length: number;
}

@Component({
  selector: 'app-role-roleby-right',
  templateUrl: './role-roleby-right.component.html'
})
export class RoleRolebyRightComponent implements OnInit {

  constructor(private server: Service, private http: HttpClient, public removeTableService: RemoveTableService) { }

  ngOnInit() {
    //接受角色Id
    this.rolebyeventEmit = this.server.rolebyeventEmit.subscribe((value: any) => {
      this.roleIdto = value;
      this.rolebyrole(this.pageNumber, this.pageSize, "0", 0);
    });
    //刷新
    this.rolebylefteventEmit = this.server.rolebylefteventEmit.subscribe((value: any) => {
      if (value) {
        // console.log("right");
        this.rolebyrole(this.pageNumber, this.pageSize, "0", 0);
      }
    });
    this.rolebyrighteventEmit = this.server.rolebyrighteventEmit.subscribe((value: any) => {
      if (value) {
        //console.log("right");
        this.rolebyrole(this.pageNumber, this.pageSize, "0", 0);
      }
    });
  }

  //取消订阅
  public rolebyeventEmit: any;
  public rolebylefteventEmit: any;
  public rolebyrighteventEmit: any;
  ngOnDestroy() {
    if (this.rolebyeventEmit || this.rolebylefteventEmit || this.rolebyrighteventEmit) {
      this.rolebyeventEmit.unsubscribe();
      this.rolebylefteventEmit.unsubscribe();
      this.rolebyrighteventEmit.unsubscribe();
    }
  }
  //双击
  @Output() outdouble = new EventEmitter();
  doublechange(val) {
    this.outdouble.emit(val);
  }
  public roleIdto: any;
  //roleby-role
  public rolebyRoleData: any[];
  public rolebyrole(pageNumber, pageSize, id, queryType): void {
    let body = {
      pageNumber: pageNumber,
      pageSize: pageSize,
      roleId: this.roleIdto,
      queryType: 0
    };
    //console.log(body)
    this.http.post<Response>(this.server.baseUrl+'admin/roles/getUserRoleList', body)
      .subscribe(res => {
        this.rolebyRoleData = res.result;
        this.totalItems = res.length;

        this.cancelSelectAll();
        //console.log(this.totalItems)
        //console.log(res)
      });
  }

  public pageState = {
    pageSize: 20,
    pageNumber: 1,
    userId: '',
    userName: '',
    userRoleId: '',
    roleId: '',
  }
  //分页
  //分页
  public totalItems: any;
  tableNumber: number = 0;
  // 当前页（默认为第一页）
  private pageNumber: number = 1;
  // 每页显示多少条数据（默认每页显示20条）
  private pageSize: number = 20;
  // id
  private id: string = '0';
  // 分页
  private pagination: Pagination;
  isLoadTableFromPaginationHandler(pagination: Pagination) {
    this.pagination = pagination;

    this.pageNumber = this.pagination[0].pageNumber;
    this.pageSize = this.pagination[0].pageSize;
    this.id = this.pagination[0].id;

    this.tableNumber = (pagination[0].pageNumber - 1) * pagination[0].pageSize;

    this.rolebyrole(this.pageNumber, this.pageSize, this.id, 0);
  }

  //全选，单选
  //  @Input() selectedItemst: string[] = [];
  //  public isSeleted(val: string, arr?: Array<string>): string | boolean {
  //    arr = arr ? arr : this.selectedItemst
  //    let isExist = arr.findIndex(each => each === val);
  //    if (isExist > -1) {
  //      return isExist + ''
  //    } else {
  //      return false
  //    }
  //  }
  //  public selectThis(val: string, arr: Array<string>): void {
  //    let key = this.isSeleted(val, arr)
  //    if (key) {
  //      arr.splice(+key, 1)
  //    } else {
  //      arr.push(val)
  //    }

  //  }
  //  public sendSelectedItems(val: string): void {
  //    this.selectThis(val, this.selectedItemst);
  //    console.log(this.selectedItemst)
  //  }
  //  public selectAll() {
  //    if (this.selectedItemst.length < this.rolebyRoleData.length) {
  //      this.selectedItemst = [];
  //      this.rolebyRoleData.map(each => {
  //        this.selectedItemst.push(each["userRoleId"])
  //      })
  //    } else {
  //      this.selectedItemst = []
  //    }
  //    console.log(this.selectedItemst)
  //  }
  //  public clearSelectedItems() {
  //    this.selectedItemst = [];
  //  }
  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  public isSelect: boolean = true;
  public isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();

  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'userRoleId');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
    //console.log(this.removeTableService.getRemoveTableId)
  }

  // 单选（多选）
  chose(bool, userRoleId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, userRoleId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
    //console.log(this.removeTableService.getRemoveTableId)
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
    this.removeTableService.getRemoveTableId = [];
  }
}
